package main;

import deduplication.carnetContacts;

import java.awt.BorderLayout;
import java.awt.Dimension;

import java.awt.Rectangle;

import java.awt.event.ComponentAdapter;

import java.awt.event.ComponentEvent;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.File;

import java.util.ArrayList;
import java.util.List;

import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;

public class Principale extends JFrame {
    
    private File file = null;
    private carnetContacts repertoire = new carnetContacts();
    private Vector<String> log = new Vector<String>();
    private BorderLayout layoutMain = new BorderLayout();
    private JPanel panelCenter = new JPanel();
    private JList jlLog = new JList(log);
    private String[] fileIn;
    private String separator;
    private String fileOut;

    Principale(String fileIn[], String separator, String fileOut) {
        this.fileIn=fileIn;
        this.separator=separator;
        this.fileOut=fileOut;
        file = new File(this.fileIn[0]);
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.getContentPane().setLayout( layoutMain );
        panelCenter.setLayout( null );
        this.setSize( new Dimension(400, 300) );
        this.setTitle( "Test deduplication" );
        this.addWindowListener(new WindowAdapter() {
                public void windowActivated(WindowEvent e) {
                    this_windowActivated(e);
                }
            });
        this.getContentPane().add( jlLog, BorderLayout.NORTH );
        this.getContentPane().add( panelCenter, BorderLayout.CENTER );
    }

    private void process() {
        log.add( "File " + file.getName()  + " selected." );
        if (!repertoire.load(file)) {
            logMsg("File " + file.getName()  + " loaded ("+ repertoire.size() +" contacts).");
            file = new File(this.fileIn[1]);
            log.add( "File " + file.getName()  + " selected." );
            if (!repertoire.load(file))
                logMsg("File " + file.getName()  + " loaded ("+ repertoire.size() +" contacts).");
            logMsg("Start of deduplication (" + repertoire.sizeTelephones() +" telephone # and "+ repertoire.sizeEmails() +" emails).");        
            repertoire.deduplicate();
            logMsg( "End of deduplication (" + repertoire.sizeTelephones() +" telephone # and "+ repertoire.sizeEmails() +" emails).");        
            file = new File(this.fileOut);
            logMsg( "File " + file.getName()  + " selected for saving." );
            repertoire.setSeparator(this.separator);
            repertoire.setStringEncloser("\"");
            repertoire.save(file);
            logMsg(repertoire.size() + " contacts saved in " + file.getName()  + "." );
        } else {
            logMsg("Error loading file " + file.getName()  + ".");
        }
    }

    private void this_windowActivated(WindowEvent e) {
        process();
        System.exit(0);
    }
    
    private void logMsg(String msg) {
        System.out.println(msg);
        log.add(msg);
        this.repaint();
    }
}
